package cn.gemiman.daoyi.module.cms.controller.admin.appsitecomment;

import cn.gemiman.daoyi.framework.apilog.core.annotation.ApiAccessLog;
import cn.gemiman.daoyi.framework.common.enums.UserTypeEnum;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageParam;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.framework.excel.core.util.ExcelUtils;
import cn.gemiman.daoyi.framework.security.core.LoginUser;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitecomment.vo.AppSiteCommentPageReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitecomment.vo.AppSiteCommentRespVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitecomment.vo.AppSiteCommentSaveReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.appsitecomment.vo.AppSiteCommentUpdateStatusReqVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitecomment.AppSiteCommentDO;
import cn.gemiman.daoyi.module.cms.service.appsitecomment.AppSiteCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.gemiman.daoyi.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;
import static cn.gemiman.daoyi.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

@Tag(name = "管理后台 - 站点评论")
@RestController
@RequestMapping("/cms/app-site-comment")
@Validated
public class AppSiteCommentController {

    @Resource
    private AppSiteCommentService appSiteCommentService;

    @PostMapping("/create")
    @Operation(summary = "创建站点评论")
    @PreAuthorize("@ss.hasPermission('cms:app-site-comment:create')")
    public CommonResult<Long> createAppSiteComment(@Valid @RequestBody AppSiteCommentSaveReqVO createReqVO) {
        createReqVO.setUserType(UserTypeEnum.ADMIN.getValue());
        LoginUser loginUser = getLoginUser();
        if (loginUser != null) {
            createReqVO.setUserId(loginUser.getId());
        }
        return success(appSiteCommentService.createAppSiteComment(createReqVO));
    }

    @PutMapping("/update-approve-status")
    @Operation(summary = "更新站点评论审核状态")
    @PreAuthorize("@ss.hasPermission('cms:app-site-comment:update')")
    public CommonResult<Boolean> updateAppSiteCommentApproveStatus(@Valid @RequestBody AppSiteCommentUpdateStatusReqVO updateReqVO) {
        appSiteCommentService.updateAppSiteCommentApproveStatus(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "更新站点评论状态")
    @PreAuthorize("@ss.hasPermission('cms:app-site-comment:update')")
    public CommonResult<Boolean> updateAppSiteCommentStatus(@Valid @RequestBody AppSiteCommentUpdateStatusReqVO updateReqVO) {
        appSiteCommentService.updateAppSiteCommentStatus(updateReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新站点评论")
    @PreAuthorize("@ss.hasPermission('cms:app-site-comment:update')")
    public CommonResult<Boolean> updateAppSiteComment(@Valid @RequestBody AppSiteCommentSaveReqVO updateReqVO) {
        appSiteCommentService.updateAppSiteComment(updateReqVO, false);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除站点评论")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:app-site-comment:delete')")
    public CommonResult<Boolean> deleteAppSiteComment(@RequestParam("id") Long id) {
        appSiteCommentService.deleteAppSiteComment(id, false);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得站点评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:app-site-comment:query')")
    public CommonResult<AppSiteCommentRespVO> getAppSiteComment(@RequestParam("id") Long id) {
        AppSiteCommentDO appSiteComment = appSiteCommentService.getAppSiteComment(id);
        return success(BeanUtils.toBean(appSiteComment, AppSiteCommentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得站点评论分页")
    @PreAuthorize("@ss.hasPermission('cms:app-site-comment:query')")
    public CommonResult<PageResult<AppSiteCommentRespVO>> getAppSiteCommentPage(@Valid AppSiteCommentPageReqVO pageReqVO) {
        PageResult<AppSiteCommentDO> pageResult = appSiteCommentService.getAppSiteCommentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppSiteCommentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出站点评论 Excel")
    @PreAuthorize("@ss.hasPermission('cms:app-site-comment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAppSiteCommentExcel(@Valid AppSiteCommentPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppSiteCommentDO> list = appSiteCommentService.getAppSiteCommentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "站点评论.xls", "数据", AppSiteCommentRespVO.class,
                BeanUtils.toBean(list, AppSiteCommentRespVO.class));
    }

}
